package com.hehe.app_test_backend.Controller;

import com.hehe.app_test_backend.Model.ResponseEntity;
import com.hehe.app_test_backend.Model.TestProject;
import com.hehe.app_test_backend.Service.TestProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.hehe.app_test_backend.Util.ResponseUtil.handleErrorResponse;
import static com.hehe.app_test_backend.Util.ResponseUtil.handleSuccessResponse;

@RequestMapping("/TestProject")
@RestController()
@CrossOrigin(origins = "*", maxAge = 3600)
public class TestProjectController {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestProjectController.class);

    @Autowired
    private TestProjectService service;

    @PostMapping("/add")
    public ResponseEntity add(@RequestBody TestProject obj) {
        try {
            service.add(obj);
            return handleSuccessResponse();
        } catch (Exception e) {
            return handleErrorResponse(e.getMessage());
        }
    }

    @PostMapping("/delete")
    public ResponseEntity delete(@RequestBody TestProject obj) {
        try {
            service.delete(obj);
            return handleSuccessResponse();
        } catch (Exception e) {
            return handleErrorResponse(e.getMessage());
        }
    }

    @PostMapping("/update")
    public ResponseEntity update(@RequestBody TestProject obj) {
        try {
            service.update(obj);
            return handleSuccessResponse();
        } catch (Exception e) {
            return handleErrorResponse(e.getMessage());
        }
    }

    @GetMapping("/getAll")
    public ResponseEntity getAll() {
        try {
            return handleSuccessResponse(service.getAll());
        } catch (Exception e) {
            return handleErrorResponse(e.getMessage());
        }
    }

    @GetMapping("/getSingle")
    public ResponseEntity getSingle(@RequestParam Integer projectId) {
        try {
            return handleSuccessResponse(service.getSingle(projectId));
        } catch (Exception e) {
            return handleErrorResponse(e.getMessage());
        }
    }
}
